//
//  ShowHTMLViewController.m
//  IOS-24
//
//  Created by    on 15/7/26.
//  Copyright (c) 2015年   . All rights reserved.
//

#import "ShowHTMLViewController.h"
#import<WebKit/WebKit.h>

@interface ShowHTMLViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
{
    NSString *pathFlag;
    NSString *tmpUrl;
    WKWebView * wkwebview1;
    NSDictionary *dict;
    NSString*SuccessURL;
}
@property (weak, nonatomic) IBOutlet UIButton *HidenShare;
- (IBAction)back:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation ShowHTMLViewController
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:NO];

   
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:NO];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = [[WKUserContentController alloc] init];

    config.preferences = [WKPreferences new];
    //The minimum font size in points default is 0;
    config.preferences.minimumFontSize = 10;
    //是否支持JavaScript
    config.preferences.javaScriptEnabled = YES;
    //不通过用户交互，是否可以打开窗口
    config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
    
    
    wkwebview1 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, DEVW, DEVH-64) configuration:config];
    wkwebview1.navigationDelegate = self;
    [self.view addSubview:wkwebview1];
    
      /* 加载本地HTML */
    if (_Htmlname) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:_Htmlname withExtension:@"html"];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        [wkwebview1 loadRequest:request];
            
    }else
    {
        /* 加载网页 */
        NSURL *url = [[NSURL alloc] initWithString:[self.urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
        //请求时忽略本地及服务端的缓存，建立新的连接
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];  //创建NSURLRequest
        [wkwebview1 loadRequest:request];
    }
    
  
    [wkwebview1 addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [wkwebview1 addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //注册通知
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 65, DEVW, 2)];
    self.progressView.backgroundColor = [UIColor blueColor];
    //设置进度条的高度，下面这句代码表示进度条的宽度变为原来的1倍，高度变为原来的1.5倍.
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.view addSubview:self.progressView];
}




- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = wkwebview1.estimatedProgress;
        if (self.progressView.progress == 1) {
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
                
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
                
            }];
            
        }
        
    } else if ([keyPath isEqualToString:@"title"])
    {
        if (object == wkwebview1) {
            _lb1.text = wkwebview1.title;
            
        } else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        
    }
    
}


- (void)dealloc {
    [wkwebview1 removeObserver:self forKeyPath:@"estimatedProgress"];
    [wkwebview1 removeObserver:self forKeyPath:@"title"];
}

-(void)didClickPopToPreviousView{
  [self.navigationController popViewControllerAnimated:YES];
}


-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    NSString *urlString = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    NSURL *WKURL = navigationAction.request.URL;
    NSString *scheme = [WKURL scheme];
    UIApplication *app = [UIApplication sharedApplication];
    // 打电话
    if ([scheme isEqualToString:@"tel"]) {
        if ([app canOpenURL:WKURL]) {
            [app openURL:WKURL];
            // 一定要加上这句,否则会打开新页面
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
    }
 
    decisionHandler(WKNavigationActionPolicyAllow);
}




//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    self.progressView.hidden = YES;
}
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = NO;
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.view bringSubviewToFront:self.progressView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)back:(id)sender {
    
    if ( ![wkwebview1 canGoBack]) {
        [self.navigationController popViewControllerAnimated:NO];
    }else
    {
        [wkwebview1 goBack];
    }
    
}
@end
